//
//  DevieEditController.m
//  DYLYJ
//
//  Created by JM on 15/11/12.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "DevieEditController.h"
#import "DeviceDetailController.h"
#import "UINavigationController+Extends.h"
#import "DeviceListController.h"
#import "DYNavigationCOntroller.h"
#import "IconSelectedController.h"
#import "DYUIKit.h"
#import "AppDelegate.h"
@interface DevieEditController ()<XPGWifiCentralControlDeviceDelegate>
@property(nonatomic,strong)IBOutlet UITextField *textField;
@end

@implementation DevieEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    if (!self.device){
         self.device = [Device deviceWithIcon:@"device_1" name:@"阳台晾衣架" state:@"设备打开" open:YES];
    }
    self.textField.text = self.device.name;
    self.imageIcon.image = [UIImage imageNamed:self.device.iconName];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    IconSelectedController *vc = [segue destinationViewController];
    vc.selectBlock = ^(NSString *aImageName){
        self.device.iconName = aImageName;
        self.imageIcon.image = [UIImage imageNamed:aImageName];
    };
}

- (IBAction)clickFinishBtn:(id)sender {
    [self.view endEditing:YES];
    self.device.name = [NSString stringWithFormat:@"%@", self.textField.text];

//    //得到设备实例
    XPGWifiDevice *device = self.device.xpgDevice;
    [XPGWifiSDK sharedInstance].delegate = self;
    if(!self.device.xpgDevice)
    {
        [MJAlertView showMessage:@"请刷新设备列表"];
        return;
    }

    if(device.did.length == 0)
    {
        [MJAlertView showMessage:@"此设备尚未与云端注 册，无法绑定，请刷新设备列表后重新尝试"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *strNameAndImage = [NSString stringWithFormat:@"%@|#$&|%@",self.device.name,self.device.iconName];
    //需要开发者自行把 uid 和 token 换成实际的值
    [[XPGWifiSDK sharedInstance] bindDeviceWithUid:myAppDelegate.uid token:myAppDelegate.token did:device.did passCode:device.passcode remark:strNameAndImage];
    
//    [[XPGWifiSDK sharedInstance] bindDeviceWithUid:myAppDelegate.uid token:myAppDelegate.token did:@"buHgCGoWYRYRWqdfkX7MeX" passCode:@"123456" remark:@"test"];

    

    
//    //todo
//    if (![self.navigationController poptoClass:[DeviceDetailController class]]) {
//        UINavigationController *navivc = self.navigationController.presentingViewController;
//        [self.navigationController dismissViewControllerAnimated:YES completion:^{
//             DeviceListController *vc = [navivc.viewControllers lastObject];
//            if ([vc isKindOfClass:[DeviceListController class]]){
//                [vc addItem:self.device];
//            }
//        }];
//    }
//    if (self.editblock) {
//        self.editblock(self.device);
//    }
}
//绑定事件
- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didBindDevice:(NSString *)did error:(NSNumber *)error errorMessage:(NSString *)errorMessage {
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
    self.device.xpgDevice.delegate = nil;
    
    if([error intValue] == XPGWifiError_NONE)
    {
        NSLog(@"绑定成功");
        //绑定成功
        //todo
        if (![self.navigationController poptoClass:[DeviceDetailController class]]) {
            UINavigationController *navivc = self.navigationController.presentingViewController;
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                DeviceListController *vc = [navivc.viewControllers lastObject];
                if ([vc isKindOfClass:[DeviceListController class]]){
//                    [vc addItem:self.device];
                }
            }];
        }
        if (self.editblock) {
            self.editblock(self.device);
        }
    }
    else
    {
        [MJAlertView showMessage:@"绑定失败"];
        NSLog(@"绑定失败:%@:%@",error,errorMessage);
        //绑定失败
    }
}


@end
